package com.vshop.service.aspecial.impl;

import com.alibaba.fastjson.JSON;
import com.vshop.config.datasource.DataSource;
import com.vshop.config.dto.ResultDataDto;
import com.vshop.config.dto.StringOutputConstant;
import com.vshop.entity.agatewaytobeijing.querydeviceorder.response.DevicePackageOrder;
import com.vshop.entity.agatewaytobeijing.querydeviceorder.response.ResponseQueryDeviceOrder;
import com.vshop.entity.agatewaytobeijing.querypartnercode.response.ResponseQueryPartnerByDeviceId;
import com.vshop.entity.alast.common.PartnerServicePlan;
import com.vshop.entity.alast.common.PartnerServicePlanSpecPrice;
import com.vshop.entity.alast.common.ServicePlanOrderRecord;
import com.vshop.entity.aspecial.activate.request.RequestFirstOrder;
import com.vshop.entity.aspecial.order.request.RequestAccessOrder;
import com.vshop.entity.aspecial.order.response.ResponseAccessOrder;
import com.vshop.entity.aspecial.querydevice.request.RequestQueryDevice;
import com.vshop.entity.aspecial.querydevice.response.ResponseQueryDevice;
import com.vshop.entity.aspecial.queryorder.request.RequestSpecialQueryOrder;
import com.vshop.entity.aspecial.queryorder.response.ResponseSpecialQueryOrder;
import com.vshop.entity.aspecial.queryorder.response.SpecialDeviceQueryOrder;
import com.vshop.entity.requestinterface.Constant;
import com.vshop.mapper.PartnerInfoMapper;
import com.vshop.mapper.alast.LogicProcessesMapper;
import com.vshop.mapper.aspecial.SpecialLogicMapper;
import com.vshop.service.PartnerInfoService;
import com.vshop.service.SequenceService;
import com.vshop.service.agatewaytobeijing.GatewayToBeijingService;
import com.vshop.service.alast.impl.LogicAssistService;
import com.vshop.service.aspecial.SpecialLogicService;
import com.vshop.service.impl.PurchaseLogicService;
import com.vshop.utils.Md5Encrypt;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 特殊的逻辑处理：所有的业务逻辑处理实现类
 * <p>
 * 现在代表客户：大阳
 *
 * @author lm
 * @date 2020/6/9 10:00
 */

@Service
@Slf4j
public class SpecialLogicServiceImpl implements SpecialLogicService {
    /**
     * 直接打开领科H5的地址
     */
    @Value("${h5.baseUrl}")
    private String baseUrl;
    @Autowired
    PurchaseLogicService purchaseService;
    @Autowired
    SpecialAssistService assistService;
    @Autowired
    LogicAssistService lAssistService;
    @Autowired
    PartnerInfoService partnerService;
    @Autowired
    SequenceService sequenceService;
    @Autowired
    GatewayToBeijingService toBeijingService;
    @Autowired
    LogicAssistService logicAssistService;

    @Resource
    private PartnerInfoMapper partnerInfoMapper;
    @Resource
    private LogicProcessesMapper logicMapper;
    @Resource
    private SpecialLogicMapper specialLogicMapper;

    @Autowired
    AfterPayDeal afterPayDeal;

    /**
     * 设备实时信息查询接口:
     * <p>
     * 1，接口响应将返回卡片是否归属领科
     * <p>
     * 2，接口响应将返回卡片接下来是应该走订购的接口还是激活的接口
     * <p>
     * 返回格式是：{"data":{},"message":"string","operateCode":"string"},ResponseQueryDevice是data部分
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDataDto queryDevice(RequestQueryDevice request) {
        try {
            if (null == request || StringUtils.isBlank(JSON.toJSONString(request))) {
                log.info("请求报文为空");
                return ResultDataDto.addOperationFailure("The request message is empty");
            }

            log.info("【设备查询】请求：" + JSON.toJSONString(request));
            if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getTradeTime()) || StringUtils.isBlank(request.getVersion()) ||
                    null == request.getLang() || StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getDeviceCode()) || StringUtils.isBlank(request.getArea()) ||
                    StringUtils.isBlank(request.getSign())) {
                log.info("请求报文不合法，存在为空情况");
                return ResultDataDto.addOperationFailure("The request parameter exists illegally");
            }

            log.info("【设备查询】请求签名：" + request.getSign());
            String sign = Md5Encrypt.jsonSign(request);
            log.info("计算的签名：" + sign);

            if (!request.getSign().equalsIgnoreCase(sign)) {
                log.info("签名不合法，请确认签名");
                return ResultDataDto.addOperationFailure("Sign illegal: Please confirm signature");
            }

            //判断卡片是否有效：1，是不是领科的卡，2，是不是不在“废旧回收期”和“内部库存期”内。
            ResponseQueryPartnerByDeviceId partner = toBeijingService.queryPartnerCode(request.getDeviceCode().trim());
            //去验证卡片
            String res = assistService.deviceVerify(partner, request.getPartnerCode(), request.getDeviceCode(), request.getLang());

            //1，确定了卡片是领科的还是非领科
            if (StringOutputConstant.CODE_ZERO.equals(res) || StringOutputConstant.CODE_ONE.equals(res)) {
                //初始化一个剩余天数，万一查不到订单时剩余天数就是0
                String remainingDays = "0";
                //返回
                ResponseQueryDevice response = new ResponseQueryDevice();
                response.setPartnerCode(request.getPartnerCode());
                response.setDeviceCode(request.getDeviceCode());
                response.setArea(request.getArea());

                //3,卡片归属标识：0-归属领科；
                if (StringOutputConstant.CODE_ZERO.equals(res)) {
                    //归属领科的卡需要再去查询订单，确定卡片到底是不是首次请求，动作也是不固定的
                    log.info(request.getDeviceCode() + "：查询【所有订单】");
                    String secret = partnerInfoMapper.getSecretByPartnerCode(request.getPartnerCode());
                    /*说明接口从2.0更新到3.0了:2021-05-24添加和修改*/
                    ResponseQueryDeviceOrder order = toBeijingService.queryDeviceOrder(request.getDeviceCode(), "", "", "", request.getPartnerCode(), secret);
                    if (null != order && Constant.RET_CODE_0000.equals(order.getCode()) && null != order.getData() && null != order.getData().getPackageOrder() && !order.getData().getPackageOrder().isEmpty()) {
                        //计算剩余天数：
                        remainingDays = assistService.retrievalRemainingDaysNew(order);
                        // 验证卡片上只有一个订单,只有这一种情况是激活
                        if (order.getData().getPackageOrder().size() == 1) {

                            Integer c = 0;

                            for (DevicePackageOrder tr : order.getData().getPackageOrder()) {
                                //拿着tr.getPackageCode()去数据库查询看看有没有这个赠送套餐的套餐编码
                                c = purchaseService.queryGiveDateOrNot(tr.getPackageCode());
                            }
                            //并且订单是赠送套餐时说明是首次来订购，这个时候可以直接给前端返回指引它走激活的接口
                            if (c >= 1) {
                                response.setMark(res);
                                response.setAction(StringOutputConstant.CODE_ACTIVATE);
                                response.setRemainingDays(remainingDays);

                                log.info("【设备查询】领科卡成功响应：" + JSON.toJSONString(response));
                                return ResultDataDto.addOperationSuccess().setData(response);
                            }
                        }
                    }
                    response.setMark(res);
                    response.setAction(StringOutputConstant.CODE_ORDER);
                    response.setRemainingDays(remainingDays);

                    log.info("【设备查询】领科卡成功返回：" + JSON.toJSONString(response));
                    return ResultDataDto.addOperationSuccess().setData(response);
                }
                //非领科卡片计算剩余天数：
                remainingDays = assistService.retrievalNonRemainingDays(request.getPartnerCode(), request.getDeviceCode());
                //非领科卡片的话直接走订购的接口
                response.setMark(res);
                response.setAction(StringOutputConstant.CODE_ORDER);
                response.setRemainingDays(remainingDays);

                log.info("【设备查询】非领科卡成功返回：" + JSON.toJSONString(response));
                return ResultDataDto.addOperationSuccess().setData(response);

            } else {
                //3，确定是领科卡片：卡片直接状态不对或者其他，总之就是不允许充值
                log.info("卡片无效等总之不允许订购或者激活：" + res);
                return ResultDataDto.addOperationFailure(res);
            }

        } catch (Exception e) {
            log.info("请求报文解析有误" + e);
            return ResultDataDto.addOperationFailure("Request message error");
        }
    }

    /**
     * 订购套餐接口：
     * <p>
     * 1，领科卡片非首次订购套餐
     * <p>
     * 2，非领科卡片无论第几次代收服务费都叫订购套餐
     * <p>
     * 3，有支付的过程
     * <p>
     * 4，非领科卡片套餐是固定的。
     * <p>
     * 5，领科卡片还是按照现有逻辑订购
     * <p>
     * 返回格式是：{"data":{},"message":"string","operateCode":"string"},ResponseAccessOrder是data部分
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDataDto topUp(RequestAccessOrder request) {
        try {
            if (null == request || StringUtils.isBlank(JSON.toJSONString(request))) {
                log.info("请求报文为空");
                return ResultDataDto.addOperationFailure("The request message is empty");
            }
            log.info("【大阳访问订购接口】请求：" + JSON.toJSONString(request));
            if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getTradeTime()) || StringUtils.isBlank(request.getVersion()) || null == request.getLang() ||
                    StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getMark()) || StringUtils.isBlank(request.getDeviceCode()) || StringUtils.isBlank(request.getArea()) ||
                    StringUtils.isBlank(request.getRequestOrderId()) || StringUtils.isBlank(request.getNoticeUrl()) || StringUtils.isBlank(request.getSign())) {
                log.info("请求报文不合法，存在为空情况");
                return ResultDataDto.addOperationFailure("The request parameter exists illegally");
            }

            log.info("【设备查询】请求签名：" + request.getSign());
            String sign = Md5Encrypt.jsonSign(request);

            if (!request.getSign().equalsIgnoreCase(sign)) {
                log.info("签名不合法，请确认签名");
                return ResultDataDto.addOperationFailure("Sign illegal: Please confirm signature");
            }

            //领科下发的打开H5的完整地址，接口调用方拿到地址启用设备默认浏览器打开即可。
            String actionUrl = baseUrl +
                    "?partnerCode=" + request.getPartnerCode() +
                    "&deviceCode=" + request.getDeviceCode() +
                    "&requestOrderId=" + request.getRequestOrderId() +
                    "&mark=" + request.getMark() +
                    "&area=" + request.getArea();

            /*2021-05-24修改:将特殊接口访问打开链接的访问记录入库*/
            assistService.insertSpecialAccessRecord(request.getTradeType(), request.getTradeTime(), request.getMark(), request.getNoticeUrl(), actionUrl, request.getRequestOrderId(), request.getDeviceCode(), request.getPartnerCode(), request.getArea());


            //给前端下发打开浏览器的地址等报文：
            ResponseAccessOrder response = new ResponseAccessOrder();
            response.setPartnerCode(request.getPartnerCode());
            response.setDeviceCode(request.getDeviceCode());
            response.setActionUrl(actionUrl);

            log.info("【大阳访问订购】成功返回：" + JSON.toJSONString(response));
            return ResultDataDto.addOperationSuccess().setData(response);

        } catch (Exception e) {
            log.info("请求报文解析有误" + e);
            return ResultDataDto.addOperationFailure("Request message error");
        }
    }


    /**
     * 激活接口：
     * <p>
     * 一定是领科卡片
     * <p>
     * 领科出卡只带一个赠送套餐首次来订购我们把这种情况定义为卡片激活:
     * <p>
     * 1，访问此接口直接异步为卡片订购套餐
     * <p>
     * 2，没有支付的过程
     * <p>
     * 3，套餐是固定的，是否需要给它做成动态的套餐。
     * <p>
     * 返回格式是：{"data":{},"message":"string","operateCode":"string"},ResponseFirstOrder是data部分
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDataDto activate(RequestFirstOrder request) {
        try {
            if (null == request || StringUtils.isBlank(JSON.toJSONString(request))) {
                log.info("请求报文为空");
                return ResultDataDto.addOperationFailure("The request message is empty");
            }
            log.info("【大阳访问激活接口】请求：" + JSON.toJSONString(request));
            if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getTradeTime()) || StringUtils.isBlank(request.getVersion()) || null == request.getLang() ||
                    StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getMark()) || StringUtils.isBlank(request.getDeviceCode()) || StringUtils.isBlank(request.getArea()) ||
                    StringUtils.isBlank(request.getRequestOrderId()) || StringUtils.isBlank(request.getNoticeUrl()) || StringUtils.isBlank(request.getSign())) {
                log.info("请求报文不合法，存在为空情况");
                return ResultDataDto.addOperationFailure("The request parameter exists illegally");
            }
            log.info("【设备查询】请求签名：" + request.getSign());
            String sign = Md5Encrypt.jsonSign(request);
            if (!request.getSign().equalsIgnoreCase(sign)) {
                log.info("签名不合法，请确认签名");
                return ResultDataDto.addOperationFailure("Sign illegal: Please confirm signature");
            }
            //先将特殊接口访问打开链接的访问记录入库
            assistService.insertSpecialAccessRecord(request.getTradeType(), request.getTradeTime(), request.getMark(), request.getNoticeUrl(), "", request.getRequestOrderId(), request.getDeviceCode(), request.getPartnerCode(), request.getArea());

            //判断卡片是否有效：1，是不是领科的卡，2，是不是不在“废旧回收期”和“内部库存期”内。
            ResponseQueryPartnerByDeviceId partner = toBeijingService.queryPartnerCode(request.getDeviceCode().trim());
            //去验证卡片
            String res = assistService.deviceVerify(partner, request.getPartnerCode(), request.getDeviceCode(), request.getLang());
            //1，确定了卡片是领科的还是非领科
            if (StringOutputConstant.CODE_ZERO.equals(res) || StringOutputConstant.CODE_ONE.equals(res)) {
                if (StringOutputConstant.CODE_ONE.equals(res)) {
                    log.info("非领科的卡片没有激活操作，应该直接走订购路线");
                    if (StringOutputConstant.CODE_LANG_EN.equals(request.getLang())) {
                        return ResultDataDto.addOperationFailure("Not a linksField card");
                    } else {
                        return ResultDataDto.addOperationFailure("非领科卡片不允许激活操作");
                    }
                }
                //2，验证卡片是不是应该走激活流程：归属领科的卡需要再去查询订单，确定卡片到底是不是首次请求，动作也是不固定的
                log.info(request.getDeviceCode() + "：查询【所有订单】");
                String secret = partnerInfoMapper.getSecretByPartnerCode(request.getPartnerCode());

                ResponseQueryDeviceOrder order = toBeijingService.queryDeviceOrder(request.getDeviceCode(), "", "", "", request.getPartnerCode(), secret);
                if (null != order && Constant.RET_CODE_0000.equals(order.getCode()) && null != order.getData() && null != order.getData().getPackageOrder() && !order.getData().getPackageOrder().isEmpty() && order.getData().getPackageOrder().size() == 1) {
                    Integer c = 0;
                    for (DevicePackageOrder tr : order.getData().getPackageOrder()) {
                        //拿着tr.getPackageCode()去数据库查询看看有没有这个赠送套餐的套餐编码
                        c = purchaseService.queryGiveDateOrNot(tr.getPackageCode());
                    }
                    //并且订单是赠送套餐时说明是首次来订购，这个时候可以直接给前端返回指引它走激活的接口
                    //只有一个订单而且订单还是赠送套餐，这个流程一定是激活操作：
                    if (c >= 1) {
                        //先确定给这个企业匹配什么语言的套餐,默认英文设为优先级高的语言
                        String languageType = lAssistService.queryLanguageType(request.getPartnerCode(), "1");
                        // 根据partnerIndex分别去分库里面取相应的卡片与套餐对应关系的数据：
                        Integer partnerIndex = Integer.parseInt(partnerService.getPartnerIndex(request.getPartnerCode()));
                        if (StringUtils.isBlank(languageType) || null == partnerIndex) {
                            log.info("languageType查询有误或者partnerIndex查询有误");
                            return ResultDataDto.addOperationFailure("Program error");
                        }
                        //直接去partner_service_plan_t拿到某个企业的服务费套餐,tags=1是内部卡首次订购适用的固定套餐标识
                        List<PartnerServicePlan> planList = lAssistService.queryServicePlanCode(partnerIndex, request.getPartnerCode(), request.getDeviceCode(), languageType, "1");
                        //如果没有匹配到套餐列表，直接返回Not Service Plan
                        if (null == planList || planList.isEmpty()) {
                            log.info("Single模式：未匹配到套餐列表,Not Service Plan");
                            return ResultDataDto.addOperationFailure("No Service,Please Contact:Service@linksfield.net");
                        }
                        //去匹配激活套餐的规格和价格和货币单位
                        String payAmount = "";
                        String payCurrency = "USD";
                        String orderPeriod = "";
                        for (PartnerServicePlan pl : planList) {
                            List<PartnerServicePlanSpecPrice> priceList = logicMapper.querySpecPrice(pl.getPartnerCode(), pl.getPackageCode(), pl.getServicePlanCode(), "1");
                            if (null == priceList || priceList.isEmpty()) {
                                log.info("未匹配到套餐规格及价格不允许展示");
                                return ResultDataDto.addOperationFailure("Not Specification And Price,Please Contact:Service@linksfield.net");
                            }
                            //取出套餐编码去匹配规格和价格
                            for (PartnerServicePlanSpecPrice price : priceList) {
                                payAmount = price.getDiscountPriceUSD();
                                orderPeriod = price.getSpecifications();
                            }
                        }
                        /*先去下单:orderNo这边自行生成：N+yyyyMMddHHMMss+4位随机数；payId这边自行生成：partnerCode+yyyyMMddHHMMss+4位随机数*/
                        String orderNo = sequenceService.getOrderNoTaskNo();
                        String payId = sequenceService.getPayIdTaskNo(request.getPartnerCode());
                        ResultDataDto result = assistService.activateOrder(planList, request, orderNo, payId, payAmount, payCurrency, orderPeriod);
                        /*确定下单的结果才能确定往下的步骤走不走*/
                        if (null != result && result.getData() != null && ResultDataDto.CODE_OPERATE_SUCCESS.equals(result.getOperateCode())) {
                            /*下单成功模拟支付成功更新订单支付状态,模拟真实支付成功回调*/
                            String transactionId = "TR-9LA790975U0359153";
                            /*看看数据库里面当前这个卡片是不是已经有成功的记录了，有记录了就不要再往下走了*/
                            Integer co = specialLogicMapper.queryActivateCount(request.getPartnerCode(), request.getDeviceCode(), transactionId, "SUCCESS");
                            /*没有成功的记录允许做一次激活*/
                            if (co < 1) {
                                Integer count = assistService.orderDataProcessing(request.getPartnerCode(), orderNo, payId, payAmount, transactionId);
                                /*异步去北京订购套餐,只有成功的支付记录入库成功才允许去*/
                                if (count > 0) {
                                    log.info(payId + ":service_plan_order_record_t更新记录成功");
                                    /*丢去redis去做订购*/
                                    afterPayDeal.addPayIdToRedis(payId);
                                    /*放入redis了就直接成功响应*/
                                    return ResultDataDto.addOperationSuccess();
                                } else {
                                    log.info("卡片激活前支付结果入库有误");
                                }
                            } else {
                                log.info("不允许重复请求卡片激活操作");
                                /*激活失败响应*/
                                if (StringOutputConstant.CODE_LANG_EN.equals(request.getLang())) {
                                    return ResultDataDto.addOperationFailure("Card does not allow repeated activation");
                                } else {
                                    return ResultDataDto.addOperationFailure("卡片不允许重复激活");
                                }
                            }
                        } else {
                            log.info("卡片反正不能走激活，原因可能很多");
                        }
                        log.info("卡片激活之前去下单失败");
                    } else {
                        log.info("不适合走激活");
                    }
                } else {
                    log.info("卡片不应该走激活");
                }
                /*激活失败响应*/
                if (StringOutputConstant.CODE_LANG_EN.equals(request.getLang())) {
                    return ResultDataDto.addOperationFailure("Activation exception. Please try again later");
                } else {
                    return ResultDataDto.addOperationFailure("激活异常，请稍后重试");
                }
            } else {
                //3，确定是领科卡片：卡片直接状态不对或者其他，总之就是不允许充值
                log.info("卡片无效等总之不允许激活：" + res);
                return ResultDataDto.addOperationFailure(res);
            }

        } catch (Exception e) {
            log.info("请求报文解析有误" + e);
            return ResultDataDto.addOperationFailure("Request message error");
        }
    }

    /**
     * 查询设备的订单接口：
     * <p>
     * 1，仅指在前端有激活或者订单的订单
     * <p>
     * 2，不管是不是领科卡片都在在深圳的数据库里面匹配
     * <p>
     * 3，两种卡片的订单信息尽量参数要做到统一
     * <p>
     * 返回格式是：{"data":{},"message":"string","operateCode":"string"},ResponseSpecialQueryOrder是data部分
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultDataDto queryOrder(RequestSpecialQueryOrder request) {
        try {
            if (null == request || StringUtils.isBlank(JSON.toJSONString(request))) {
                log.info("请求报文为空");
                return ResultDataDto.addOperationFailure("The request message is empty");
            }
            log.info("【大阳访问激活接口】请求：" + JSON.toJSONString(request));
            if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getTradeTime()) || StringUtils.isBlank(request.getVersion()) ||
                    StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getMark()) || StringUtils.isBlank(request.getDeviceCode()) || StringUtils.isBlank(request.getSign())) {
                log.info("请求报文不合法，存在为空情况");
                return ResultDataDto.addOperationFailure("The request parameter exists illegally");
            }
            log.info("【设备查询】请求签名：" + request.getSign());
            String sign = Md5Encrypt.jsonSign(request);
            if (!request.getSign().equalsIgnoreCase(sign)) {
                log.info("签名不合法，请确认签名");
                return ResultDataDto.addOperationFailure("Sign illegal: Please confirm signature");
            }
            //此查询订单接口仅查询设备在领科H5支付订购或者激活的订单
            List<ServicePlanOrderRecord> recordList = specialLogicMapper.queryRecordBySpecialDevice(request.getPartnerCode(), request.getDeviceCode());
            ResponseSpecialQueryOrder response = new ResponseSpecialQueryOrder();
            if (null != recordList && !recordList.isEmpty()) {
                List<SpecialDeviceQueryOrder> orderList = new ArrayList<>();
                SpecialDeviceQueryOrder order = null;
                for (ServicePlanOrderRecord record : recordList) {
                    order = new SpecialDeviceQueryOrder();
                    order.setServicePlanShowName(record.getServicePlanShowName());
                    order.setOrderPeriod(record.getOrderPeriod() + assistService.setUpOrderPeriodUnit(record.getPackageType()));
                    order.setPayId(record.getPayId());
                    order.setRequestOrderId(record.getRequestOrderId());
                    order.setPayType(assistService.setUpOrderPayType(record.getPayType()));
                    order.setPayAmount(record.getPayAmount());
                    order.setPayCurrency(record.getPayCurrency());
                    order.setPayRst(record.getPayRst());
                    order.setPayResultDate(record.getPayResultDate());
                    orderList.add(order);
                }
                //装载返回
                response.setOrderList(orderList);
                response.setPartnerCode(request.getPartnerCode());
                response.setDeviceCode(request.getDeviceCode());
                log.info("特殊查询订单成功响应：" + JSON.toJSONString(response));
                return ResultDataDto.addOperationSuccess().setData(response);
            }
            log.info("特殊查询订单：" + request.getDeviceCode() + "：无订购订单记录");
            return ResultDataDto.addOperationSuccess("No Order Records").setData(response);
        } catch (Exception e) {
            log.info("请求报文解析有误" + e);
            return ResultDataDto.addOperationFailure("Request message error");
        }
    }

    /**
     * 根据企业编码去查询数据库里面需要给企业推送的订单数据
     *
     * @param partnerCode
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ServicePlanOrderRecord> queryChannelResultPushOrderWithCode(String partnerCode) {
        return specialLogicMapper.queryChannelResultPushOrderWithCode(partnerCode);
    }

    /**
     * 根据企业编码列表去查询数据库里面需要给企业推送的订单数据
     *
     * @param codeList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<ServicePlanOrderRecord> queryChannelResultPushOrderWithCodeList(List<String> codeList) {
        return specialLogicMapper.queryChannelResultPushOrderWithCodeList(codeList);
    }

    /**
     * 根据企业和设备和第三方自己的订单id去查询回调noticeUrl
     *
     * @param partnerCode
     * @param deviceCode
     * @param requestOrderId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String queryNoticeUrl(String partnerCode, String deviceCode, String requestOrderId) {
        return specialLogicMapper.queryNoticeUrl(partnerCode, deviceCode, requestOrderId);
    }

    /**
     * 更新推送某条记录的提示状态 :
     * isPartnerNotice 是否做企业同步：1-通知，0-不用，默认“1”
     *
     * @param partnerCode
     * @param deviceCode
     * @param payId
     * @param orderNo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateNoticeRecord(String partnerCode, String deviceCode, String payId, String orderNo) {
        return specialLogicMapper.updateNoticeRecord(partnerCode, deviceCode, payId, orderNo);
    }


}
